﻿#region - 版 本 注 释 -
/*----------------------------------------------------------------
 * 版权所有 (c) 2024   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-GLJ2Q8J
 * 公司名称：
 * 命名空间：AutoCAD.EntityTool
 * 唯一标识：cd35dde5-61e6-44d3-b65c-7a302677ae89
 * 文件名：ShowMessageTools
 * 当前用户域：DESKTOP-GLJ2Q8J
 * 
 * 创建者：Guo-79991
 * 电子邮箱：799919859@qq.com
 * 创建时间：2024/7/15 12:04:57
 * 版本：V1.0.0
 *
 * 功能描述(Description):
 * 参考文档(Reference):
 * 数据表(Tables):
 * 作者(Author):Guo
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion - 版 本 注 释 -


namespace AutoCAD.EntityTools
{
    public static class StringTools
    {
        /// <summary>
        /// 将Point3d转换为标准坐标字符串 (X,Y,Z)
        /// </summary>
        /// <param name="point">三维点</param>
        /// <param name="decimalPlaces">小数位数(默认3位)</param>
        /// <returns>格式化的坐标字符串</returns>
        public static string ToCoordinateString(this Point3d point, int decimalPlaces = 3)
        {
            string format = $"F{decimalPlaces}";
            return $"{point.X.ToString(format)},{point.Y.ToString(format)},{point.Z.ToString(format)}";
        }

        /// <summary>
        /// 将Point2d转换为标准坐标字符串 (X,Y)
        /// </summary>
        /// <param name="point">二维点</param>
        /// <param name="decimalPlaces">小数位数(默认3位)</param>
        /// <returns>格式化的坐标字符串</returns>
        public static string ToCoordinateString(this Point2d point, int decimalPlaces = 3)
        {
            string format = $"F{decimalPlaces}";
            return $"{point.X.ToString(format)},{point.Y.ToString(format)}";
        }
        public static string ProcessNewLines(this string input)
        {
            try
            {
                if (string.IsNullOrEmpty(input))
                {
                    return input;
                }

                return input.Replace("\\n", Environment.NewLine).Replace("&#xA;", Environment.NewLine);
            }
            catch (Exception ex)
            {
                ex.Message.CadMessage2();
                return "";
            }
        }
        /// <summary>
        /// 从字符串解析Point3d (支持X,Y,Z或X,Y格式)
        /// </summary>
        /// <param name="coordString">坐标字符串</param>
        /// <returns>解析后的Point3d</returns>
        public static Point3d ParsePoint3d(string coordString)
        {
            if (string.IsNullOrWhiteSpace(coordString))
                throw new ArgumentException("坐标字符串不能为空");

            string[] parts = coordString.Split(',');
            if (parts.Length < 2 || parts.Length > 3)
                throw new FormatException("坐标格式应为X,Y或X,Y,Z");

            double x = double.Parse(parts[0].Trim());
            double y = double.Parse(parts[1].Trim());
            double z = parts.Length == 3 ? double.Parse(parts[2].Trim()) : 0;

            return new Point3d(x, y, z);
        }

        /// <summary>
        /// 获取点的X坐标字符串
        /// </summary>
        /// <param name="point">三维点</param>
        /// <param name="decimalPlaces">小数位数</param>
        /// <returns>X坐标字符串</returns>
        public static string GetXString(this Point3d point, int decimalPlaces = 3)
        {
            return point.X.ToString($"F{decimalPlaces}");
        }

        /// <summary>
        /// 获取点的Y坐标字符串
        /// </summary>
        /// <param name="point">三维点</param>
        /// <param name="decimalPlaces">小数位数</param>
        /// <returns>Y坐标字符串</returns>
        public static string GetYString(this Point3d point, int decimalPlaces = 3)
        {
            return point.Y.ToString($"F{decimalPlaces}");
        }

        /// <summary>
        /// 获取点的Z坐标字符串
        /// </summary>
        /// <param name="point">三维点</param>
        /// <param name="decimalPlaces">小数位数</param>
        /// <returns>Z坐标字符串</returns>
        public static string GetZString(this Point3d point, int decimalPlaces = 3)
        {
            return point.Z.ToString($"F{decimalPlaces}");
        }

        /// <summary>
        /// 将点转换为WKT(Well-Known Text)格式
        /// </summary>
        /// <param name="point">三维点</param>
        /// <returns>WKT格式字符串</returns>
        public static string ToWktString(this Point3d point)
        {
            return $"POINT ({point.X} {point.Y} {point.Z})";
        }

        /// <summary>
        /// 将点转换为JSON格式
        /// </summary>
        /// <param name="point">三维点</param>
        /// <returns>JSON格式字符串</returns>
        public static string ToJsonString(this Point3d point)
        {
            return $"{{\"x\":{point.X},\"y\":{point.Y},\"z\":{point.Z}}}";
        }

        /// <summary>
        /// 计算两点之间的距离
        /// </summary>
        /// <param name="point1">第一个点</param>
        /// <param name="point2">第二个点</param>
        /// <returns>两点之间的距离</returns>
        public static double DistanceTo(this Point3d point1, Point3d point2)
        {
            return point1.DistanceTo(point2);
        }

        /// <summary>
        /// 将Point3d转换为Point2d(丢弃Z坐标)
        /// </summary>
        /// <param name="point">三维点</param>
        /// <returns>二维点</returns>
        public static Point2d ToPoint2d(this Point3d point)
        {
            return new Point2d(point.X, point.Y);
        }

        /// <summary>
        /// 将Point2d转换为Point3d(Z坐标设为0)
        /// </summary>
        /// <param name="point">二维点</param>
        /// <returns>三维点</returns>
        public static Point3d ToPoint3d(this Point2d point)
        {
            return new Point3d(point.X, point.Y, 0);
        }
    }
}

/*
         * 使用示例
        csharp
        // 创建点
        Point3d point = new Point3d(123.456789, 987.654321, 5.5);

        // 转换为坐标字符串
        string coordString = point.ToCoordinateString(); // "123.457,987.654,5.500"
        string coordString2 = point.ToCoordinateString(2); // "123.46,987.65,5.50"

        // 获取单个坐标
        string xStr = point.GetXString(); // "123.457"
        string yStr = point.GetYString(1); // "987.7"

        // 转换为不同格式
        string wkt = point.ToWktString(); // "POINT (123.456789 987.654321 5.5)"
        string json = point.ToJsonString(); // "{"x":123.456789,"y":987.654321,"z":5.5}"

        // 从字符串解析点
        Point3d parsedPoint = CadPointUtils.ParsePoint3d("100,200,5");

        // 二维点转换
        Point2d point2d = point.ToPoint2d();
        Point3d point3d = point2d.ToPoint3d();

        // 计算距离
        Point3d anotherPoint = new Point3d(100, 200, 0);
        double distance = point.DistanceTo(anotherPoint);
 */
